package com.amigo.online.provider.adv.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.amigo.online.common.param.adv.bo.AdvHomeInitBO;

@Mapper
public interface AdvHomeApplyMapper {

    @Select("select id, target_user_sex_type as sexType, target_user_age_type as sexType, target_area_type as areaType,"
            + " show_type as showType, total_number as remainingNumber from adv_home_apply_tbl  where end_time = #{timeMillis}"
            + " and status = 1 and deleted = 0")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "areaList", column = "id", javaType = List.class
                    , many = @Many(select = "com.amigo.online.provider.adv.mapper.AdvApplyAreaMapper.getHomeApplyAreaIdsByApplyId"))
    })
    List<AdvHomeInitBO> listFindTomorrowAdvSettlePlanByEndTime(@Param("timeMillis") long today);

    @Select("select id, target_user_sex_type as sexType, target_user_age_type as ageType, target_area_type as areaType,"
            + " show_type as showType, total_number as remainingNumber from adv_home_apply_tbl where start_time <= #{timeMillis}"
            + " and end_time >= #{timeMillis} " +
            "and status = 1 and deleted = 0")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "areaList", column = "id", javaType = List.class
                    , many = @Many(select = "com.amigo.online.provider.adv.mapper.AdvApplyAreaMapper.getHomeApplyAreaIdsByApplyId"))
    })
    List<AdvHomeInitBO> listFindTomorrowAdvDeliveryPlanByStartTime(@Param("timeMillis") long tomorrow);

    @Select("SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\tadv_home_apply_tbl hat\n" +
            "\tLEFT JOIN adv_home_apply_area_tbl haat ON hat.id = haat.adv_id \n" +
            "WHERE\n" +
            "\tarea_id = 10033 \n" +
            "\tAND start_time <= #{timeMillis}\n" +
            "\tAND end_time >= #{timeMillis}")
    List<AdvHomeInitBO> listFindTomorrowAdvDeliveryPlanByStartTime2(@Param("area_id") long area_id,long timeMillis);
}
